/**
 * 计算机AI实现
 */
// 攻防策略分数定义
function deffence(num) {
    let pts = {
        0: 0,
        1: 200,
        2: 400,
        3: 2000,
        4: 10000
    }
    if (num > 4) {
        return 0;
    } else {
        return pts[num];
    }
}
function attack(num) {
    let pts = {
        0: 0,
        1: 210,
        2: 420,
        3: 2100,
        4: 20000
    }
    if (num > 4) {
        return 0;
    } else {
        return pts[num];
    }
}

// AI
function computerAI(player) {
    // 判断黑白方
    let opponent = black, self = white;
    if(!player){
        opponent = white;
        self = black;
    }
    // 得分数组
    let person = [];
    let com = [];
    // 初始化
    for (let i = 0; i < lineCounts; i++) {
        person[i] = [];
        com[i] = [];
        for (let j = 0; j < lineCounts; j++) {
            person[i][j] = 0;
            com[i][j] = 0;
        }
    }
    // 判断各个落点得分
    for (let i = 0; i < lineCounts; i++) {
        for (let j = 0; j < lineCounts; j++) {
            if (pieceMatrix[i][j]) { // 可落子
                for (let k = 0; k < counts; k++) {
                    if (winMatrix[i][j][k]) { // 可赢
                        person[i][j] += deffence(opponent[k]); // 防守
                        com[i][j] += attack(self[k]); // 进攻
                    }
                }
            }
        }
    }
    // 寻找最优点
    var max = 0; // 最高分
    var u = 0, v = 0; // 最优落子点
    for (let i = 0; i < lineCounts; i++) {
        for (let j = 0; j < lineCounts; j++) {
            // 防守最高分
            if (person[i][j] > max) {
                max = person[i][j];
                u = i;
                v = j;
            }
            // 进攻最高分
            if (com[i][j] > max) {
                max = com[i][j];
                u = i;
                v = j;
            }
        }
    }
    // 电脑落子
    oneStep(u, v, !player);
}